package com.a16.yuejian.yuejian.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.Clean;
import com.a16.yuejian.yuejian.activity.MyFollowActivity;
import com.a16.yuejian.yuejian.activity.MyMessageActivity;
import com.a16.yuejian.yuejian.activity.MyOrderActivity;
import com.a16.yuejian.yuejian.activity.MyPostActivity;
import com.a16.yuejian.yuejian.activity.MySuggestActivity;
import com.a16.yuejian.yuejian.javabean.User;
import com.a16.yuejian.yuejian.listener.UserListener;
import com.a16.yuejian.yuejian.model.UserModel;
import com.a16.yuejian.yuejian.roundimagevIew.RoundImageView;
import com.squareup.picasso.Picasso;

public class MyFragment extends Fragment {
    private RoundImageView my_user_pic;
    private TextView my_user_name,my_mycomment,my_myfollow,my_myorder,huanchun;
    private ImageView my_right,my_right2,my_right3,my_suggest;
    private LinearLayout my_clear;
    private RecyclerView recyclerview;
    private SharedPreferences sp;
    private View view=null;


    UserListener userListener = new UserListener() {
        @Override
        public void onResponse(User userinfo) {
            Picasso.with(getActivity()).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+userinfo.getPic()).into(my_user_pic);
            my_user_name.setText(userinfo.getUsername());
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.fragment_my, container, false);
        initRecyclerView(view);
        getData();
        abc();
        initEvent();
        return view;
    }

    private void abc() {
        try {
            huanchun.setText(Clean.getTotalCacheSize(getActivity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void getData() {
        UserModel userModel = new UserModel();
        userModel.userinfo(sp.getString("uid",""),userListener);
    }


    private void initEvent() {
        my_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getActivity()).setTitle("确定清理缓存")
                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Clean.clearAllCache(getActivity());
                                abc();
                            }
                        })
                        .setPositiveButton("取消", null)
                        .show();
            }
        });
        my_mycomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyPostActivity.class);
                getActivity().startActivity(intent);
            }
        });

        my_myfollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyFollowActivity.class);
                getActivity().startActivity(intent);
            }
        });

        my_myorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyOrderActivity.class);
                getActivity().startActivity(intent);
            }
        });

        my_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyMessageActivity.class);
                getActivity().startActivity(intent);
            }
        });

        my_suggest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),MySuggestActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }


    private void initRecyclerView(View view) {
        my_right = (ImageView)view.findViewById(R.id.my_right);
        my_right2 = (ImageView)view.findViewById(R.id.my_right2);
        my_right3 = (ImageView)view.findViewById(R.id.my_right3);
        my_suggest = (ImageView)view.findViewById(R.id.my_suggest);
        my_user_pic = (RoundImageView)view.findViewById(R.id.my_user_pic);
        my_user_name = (TextView)view.findViewById(R.id.my_user_name);
        my_myorder = (TextView)view.findViewById(R.id.my_myorder);
        my_myfollow = (TextView)view.findViewById(R.id.my_myfollow);
        my_mycomment = (TextView)view.findViewById(R.id.my_mycomment);
        huanchun = (TextView)view.findViewById(R.id.huanchun);
        my_clear = (LinearLayout)view.findViewById(R.id.my_clear);
        sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }



}
